/**
 * @Copyright:Copyright (c) 1992 - 2016
 * @Company: TengTong
 */

package com.tengtong.duang.commons.base.impl;

import java.util.Iterator;
import java.util.function.BiConsumer;

import com.tengtong.duang.commons.base.Range;

/**
 * 范围
 * 
 * @author Jai
 *
 */
public class IntegerRange implements Range<Integer>{
	
	private final Integer begin;
	
	private final Integer end;
	
	/**
	 * 表示 [0, end] 的范围
	 * @param end
	 */
	public IntegerRange(int end){
		this(0, end);
	}
	
	/**
	 * 表示 [begin, end] 的范围
	 * 
	 * @param begin
	 * @param end
	 */
	public IntegerRange(int begin, int end){
		this.begin = begin;
		this.end   = end;
	}
	
	public final static IntegerRange range(int begin, int end){
		return new IntegerRange(begin, end);
	}

	/**
	 * 判断是否在内，但不包括边界，即 (begin, end)
	 *
	 * @param data
	 * @return
	 */
	public boolean in(int data){
		return (data > begin) && (data < end);
	}
	
	/**
	 * 判断是否在内，包括边界，即 [begin, end]
	 *
	 * @param data
	 * @return
	 */
	public boolean contains(int data){
		return (data >= begin) && (data <= end);
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Iterable#iterator()
	 */
	@Override
	public Iterator<Integer> iterator() {
		return new Iterator<Integer>(){
			
			private int curr = begin;

			@Override
			public boolean hasNext() {
				return curr <= end;
			}

			@Override
			public Integer next() {
				return curr++;
			}
			
		};
	}

	/* (non-Javadoc)
	 * @see com.tengtong.duang.commons.base.Range#getBegin()
	 */
	@Override
	public Integer getBegin() {
		return begin;
	}

	/* (non-Javadoc)
	 * @see com.tengtong.duang.commons.base.Range#getEnd()
	 */
	@Override
	public Integer getEnd() {
		return end;
	}

}
